import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lcapp/api/interface/comcon_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/basic/user_info.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/data/comment_data_entity.dart';
import 'package:lcapp/model/toast/toast.dart';
import 'package:lcapp/routes/route.dart';
import 'package:lcapp/widget/asr_cache_image.dart';
import 'package:lcapp/widget/lc_gesture_detector.dart';

class CommentItem extends StatefulWidget {
  final CommentDataEntity? entity;
  final Function? commentFun;
  const CommentItem({Key? key, this.entity, this.commentFun}) : super(key: key);

  @override
  _CommentItemState createState() => _CommentItemState();
}

class _CommentItemState extends State<CommentItem> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      margin: EdgeInsets.only(bottom: 40.w),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          GestureDetector(
            onTap: (){
              FocusScope.of(context).requestFocus(FocusNode());
              if(userInfo!=null&&userInfo.id!=widget.entity?.userId){
                SmartDialog.dismiss();
                Navigator.of(ASRRoute.navigatorKey.currentContext!).pushNamed("ShopHomePage",arguments: widget.entity?.userId);
              }
            },
            child: ClipRRect(
              borderRadius: BorderRadius.circular(90.w),
              child: ASRCacheImage(
                image: widget.entity?.image??'assets/icon/p_head.png',
                fit: BoxFit.fitWidth,
                height: 90.w,
                width: 90.w,
              ),
            ),
          ),
          Expanded(
              child: Column(
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Expanded(child: GestureDetector(
                        onTap: (){
                          if(widget.commentFun!=null){
                            widget.commentFun!(widget.entity!.id!,widget.entity?.userName??'');
                          }
                        },
                        child: Container(
                          color: Colors.transparent,
                          child: Column(
                            children: [
                              Row(
                                children: [
                                  Text(widget.entity?.userName??'',style: TextStyle(
                                      color: Color(0xff505050),
                                      fontSize: 26.sp
                                  ),),
                                  SizedBox(width:10.w),
                                  Container(
                                    width: widget.entity?.author ==1? 55.w:0.w,
                                    height: 26.sp,
                                    decoration: BoxDecoration(
                                        color: defaultBarColor,
                                        borderRadius: BorderRadius.circular(5)
                                    ),
                                    alignment: Alignment.center,
                                    child: Text('商家',style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 16.sp
                                    ),),
                                  ),
                                  // Image.asset("assets/home/aothor.png",width: widget.entity.author ==1? 50.w:0,),
                                ],
                              ),
                              SizedBox(height: 15.w,),
                              Text(widget.entity?.content??'',style: TextStyle(
                                  color: Colors.black,
                                  fontSize: 28.sp
                              ),),
                              SizedBox(height: 15.w,),
                              Text('${widget.entity?.timeDiffer}  ${widget.entity?.address??''}',style: TextStyle(
                                  color: Color(0xff999999),
                                  fontSize: 24.sp
                              ),),
                            ],
                            crossAxisAlignment: CrossAxisAlignment.start,
                          ),
                          padding: EdgeInsets.symmetric(horizontal: 20.w,vertical: 10.w),
                        ),
                      ),),
                      lcGestureDetector(
                        isLogin:true,
                        onTap: ()async{
                          showLoading();
                          ResponseData res = await CommonAPI.commentLike(widget.entity!.id!, widget.entity!.operation!);
                          hideLoading();
                          if(res.code==200){
                            setState(() {
                              if(widget.entity!.operation==1){
                                widget.entity!.operation = 0;
                                widget.entity!.likes = widget.entity!.likes! - 1;
                                showToast('已取消点赞');
                              }else{
                                showToast('已点赞');
                                widget.entity!.operation = 1;
                                widget.entity!.likes = widget.entity!.likes! + 1;
                              }
                            });
                          }
                        },
                        child: Container(
                          child: Column(
                            children: [
                              Image.asset(widget.entity!.operation==1?"assets/home/pl_dz.png":"assets/home/pl_dz_ys.png",width: 60.w,),
                              SizedBox(height: 10.w,),
                              Text('${widget.entity!.likes}',style: TextStyle(
                                  color: Color(0xff999999),
                                  fontSize: 22.sp
                              ),)
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                  Visibility(child: ListView.builder(
                      padding: EdgeInsets.zero,
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      itemCount: widget.entity!.replies==null?0:widget.entity!.replies!.length,
                      itemBuilder: (context,index){
                        CommentDataEntity rentity = widget.entity!.replies![index];
                        return Container(
                          margin: EdgeInsets.only(top: 20.w),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              GestureDetector(
                                onTap: (){
                                  FocusScope.of(context).requestFocus(FocusNode());
                                  if(userInfo!=null&&userInfo.id!=rentity.userId){
                                    SmartDialog.dismiss();
                                    Navigator.of(ASRRoute.navigatorKey.currentContext!).pushNamed("ShopHomePage",arguments: rentity.userId);
                                  }
                                },
                                child: ClipRRect(
                                  borderRadius: BorderRadius.circular(60.w),
                                  child: ASRCacheImage(
                                    image: rentity.image??'assets/icon/p_head.png',
                                    fit: BoxFit.fitWidth,
                                    height: 60.w,
                                    width: 60.w,
                                  ),
                                ),
                              ),
                              Expanded(
                                  child: Container(
                                    child: Column(
                                      children: [
                                        Row(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Expanded(child: GestureDetector(
                                              onTap: (){
                                                if(widget.commentFun!=null){
                                                  widget.commentFun!(rentity.id,rentity.userName??'');
                                                }
                                              },
                                              child: Container(
                                                color:Colors.transparent,
                                                child: Column(
                                                  children: [
                                                    Row(
                                                      children: [
                                                        Text(rentity.userName??'',style: TextStyle(
                                                            color: Color(0xff505050),
                                                            fontSize: 26.sp
                                                        ),),
                                                        SizedBox(width:10.w),
                                                        // Image.asset("assets/home/aothor.png",width: rentity.author ==1? 50.w:0,),
                                                        Container(
                                                          width: rentity.author ==1? 55.w:0.w,
                                                          height: 26.sp,
                                                          decoration: BoxDecoration(
                                                              color: defaultBarColor,
                                                              borderRadius: BorderRadius.circular(5)
                                                          ),
                                                          alignment: Alignment.center,
                                                          child: Text('商家',style: TextStyle(
                                                              color: Colors.white,
                                                              fontSize: 16.sp
                                                          ),),
                                                        ),
                                                        Icon(Icons.arrow_right,color: Color(0xff666666),size: 30.w,),
                                                        Text(rentity.toUserName??'',style: TextStyle(
                                                            color: Color(0xff505050),
                                                            fontSize: 26.sp
                                                        ),),
                                                        SizedBox(width:10.w),
                                                        // Image.asset("assets/home/aothor.png",width: rentity.author ==1? 50.w:0,),
                                                        Container(
                                                          width: rentity.author ==1? 55.w:0.w,
                                                          height: 26.sp,
                                                          decoration: BoxDecoration(
                                                              color: defaultBarColor,
                                                              borderRadius: BorderRadius.circular(5)
                                                          ),
                                                          alignment: Alignment.center,
                                                          child: Text('商家',style: TextStyle(
                                                              color: Colors.white,
                                                              fontSize: 16.sp
                                                          ),),
                                                        ),
                                                      ],
                                                    ),
                                                    SizedBox(height: 15.w,),
                                                    Text(rentity.content??'',style: TextStyle(
                                                        color: Colors.black,
                                                        fontSize: 28.sp
                                                    ),),
                                                    SizedBox(height: 15.w,),
                                                    Text('${rentity.timeDiffer}  ${rentity.address??''}',style: TextStyle(
                                                        color: Color(0xff999999),
                                                        fontSize: 24.sp
                                                    ),),
                                                  ],
                                                  crossAxisAlignment: CrossAxisAlignment.start,
                                                ),
                                                padding: EdgeInsets.symmetric(horizontal: 20.w,vertical: 10.w),
                                              ),
                                            ),),
                                            lcGestureDetector(
                                              isLogin:true,
                                              onTap: ()async{
                                                showLoading();
                                                ResponseData res = await CommonAPI.commentReplyLike(rentity.id!, rentity.operation!);
                                                hideLoading();
                                                if(res.code==200){
                                                  setState(() {
                                                    if(rentity.operation==1){
                                                      rentity.operation = 0;
                                                      rentity.likes = rentity.likes! -1;
                                                      showToast('已取消点赞');
                                                    }else{
                                                      rentity.operation = 1;
                                                      rentity.likes = rentity.likes! +1;
                                                      showToast('已点赞');
                                                    }
                                                  });
                                                }
                                              },
                                              child: Container(
                                                child: Column(
                                                  children: [
                                                    Image.asset(rentity.operation==1?"assets/home/pl_dz.png": "assets/home/pl_dz_ys.png",width: 45.w,),
                                                    SizedBox(height: 10.w,),
                                                    Text('${rentity.likes}',style: TextStyle(
                                                        color: Color(0xff999999),
                                                        fontSize: 22.sp
                                                    ),)
                                                  ],
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ],
                                    ),
                                  )
                              ),

                            ],
                          ),
                        );
                      }
                  ),),
                  Visibility(
                      child: GestureDetector(
                        onTap: ()async{
                          ResponseData res = await CommonAPI.getCommentReplies(widget.entity!.id!,1,widget.entity!.reply!);
                          if(res.code==200){
                            setState(() {
                              List arr = res.data??[];
                              widget.entity!.replies = [];
                              arr.forEach((element) {
                                CommentDataEntity dataEntity = CommentDataEntity().fromJson(element);
                                dataEntity.replies = [];
                                widget.entity!.replies!.add(dataEntity);
                              });
                            });
                          }
                        },
                        child: Container(
                          margin: EdgeInsets.only(top: 10.w),
                          child: Row(
                            children: [
                              Container(
                                width: 50.w,
                                height: 0.5,
                                color: Color(0xff999999),
                              ),
                              Container(
                                child: Text('  展开${widget.entity!.reply}条评论  ',style: TextStyle(
                                    color: Color(0xff6D6D6D),
                                    fontSize: 26.sp
                                ),),
                              ),
                              Container(
                                child: Icon(Icons.keyboard_arrow_down,size: 30.w,color: Color(0xff999999),),
                              ),
                            ],
                          ),
                        ),
                      ),
                       visible: widget.entity!.reply!=0&&widget.entity!.reply!=widget.entity!.replies!.length,
                  ),
                ],
              )
          ),
        ],
      ),
    );
  }
}
